package com.winkyle.woodenfish.controller.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import com.winkyle.woodenfish.R;
import com.winkyle.woodenfish.adapter.MessageAdapter;
import com.winkyle.woodenfish.controller.activity.LoadUrlActivity;
import com.winkyle.woodenfish.databinding.FragmentMessageBinding;
import com.winkyle.woodenfish.entity.MessageInfo;
import com.winkyle.woodenfish.factory.presenterfactory.CreatePresenter;
import com.winkyle.woodenfish.listener.PullUpLoadMoreListener;
import com.winkyle.woodenfish.mvpview.MessageView;
import com.winkyle.woodenfish.presenter.MessagePresenter;

import java.util.List;

/**
 * @author winkyle
 * @date 2018/06/20
 * @description 继承自 {@link AbstractMvpFragment}的碎片类MessageFragment，app的我的消息页
 */
@CreatePresenter(value = MessagePresenter.class)
public class MessageFragment extends AbstractMvpFragment<MessageView, MessagePresenter> implements MessageView {

    private FragmentMessageBinding fragmentMessageBinding;

    /**
     * @param inflater
     * @param container
     * @param savedInstanceState 保存的碎片状态的数据
     * @return
     * @description 碎片创建，指定layout布局
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        fragmentMessageBinding = DataBindingUtil.bind(view);
        initFragment();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(messageAdapter.getMessageInfos()!=null&&!messageAdapter.getMessageInfos().isEmpty()) {
            return;
        }
        if(isLogin()) {
            getMvpPresenter().viewMessage(1);
        }
    }

    private MessageAdapter messageAdapter;
    private PullUpLoadMoreListener pullUpLoadMoreListener;

    /**
     * @return
     * @description 初始化碎片的一些数据和动作
     */
    private void initFragment() {
        fragmentMessageBinding.setMessageView(this);
        fragmentMessageBinding.setNothing(true);
        fragmentMessageBinding.swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getMvpPresenter().viewMessage(1);
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        fragmentMessageBinding.message.setLayoutManager(linearLayoutManager);
        messageAdapter = new MessageAdapter(getContext());
        messageAdapter.setOnClickLinkListener(new MessageAdapter.OnClickLinkListener() {
                                                  @Override
                                                  public void onClick(MessageInfo messageInfo) {
                                                      startActivity(LoadUrlActivity.class,"url",messageInfo.getLinkAddress());
                                                  }
                                              }
        );
        fragmentMessageBinding.message.setAdapter(messageAdapter);
        pullUpLoadMoreListener = new PullUpLoadMoreListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                getMvpPresenter().viewMessage(currentPage);
            }
        };
        fragmentMessageBinding.message.addOnScrollListener(pullUpLoadMoreListener);
    }

    private boolean frist = true;
    @Override
    public void refreshMessage(List<MessageInfo> messageInfos) {
        if (fragmentMessageBinding.swipeRefresh.isRefreshing()) {
            messageAdapter.clearMessageInfos();
            pullUpLoadMoreListener.initState();
            fragmentMessageBinding.swipeRefresh.setRefreshing(false);
            fragmentMessageBinding.setNothing(true);
        }
        if (messageInfos.size() <= 0) {
            return;
        }
        frist = false;
        fragmentMessageBinding.setNothing(false);
        for (MessageInfo messageInfo : messageInfos) {
            messageAdapter.addMessageInfo(messageInfo);
        }
    }

    @Override
    public void refresh(View view) {
        RotateAnimation refreshRotateAnimation = new RotateAnimation(0, 720,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        refreshRotateAnimation.setDuration(500);
        refreshRotateAnimation.setFillEnabled(true);
        refreshRotateAnimation.setFillAfter(true);
        view.startAnimation(refreshRotateAnimation);
        getMvpPresenter().viewMessage(1);
    }


}
